﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjetSMA.View._2D
{
    public partial class Map2D : Form
    {

        private WorldModel m_model;
        private int m_x = 50, m_y = 50;
        private List<Element2D> m_list_elements;
        private List<ItemMove2D> m_item_move;
        private Timer m_timer = new Timer();
        private Point m_point = new Point(0);
        private int m_limit_grid = 1;
        private int m_time = 100;

        public Map2D(WorldModel par_model)
        {
            InitializeComponent();

            m_model = par_model;
            par_model.SetUp( 30 );
            par_model.MyWorld.InitialisationMap();

            InitializeModel(par_model);


            m_timer.Tick += new EventHandler(timer_Tick);
            m_timer.Interval = (m_time) * (1);
            m_timer.Enabled = true;
            m_timer.Start();

        }

        private void InitializeModel(WorldModel par_model)
        {
            m_list_elements = new List<Element2D>();
            m_item_move = new List<ItemMove2D>();

            for (int i = 0; i < par_model.MyWorld.LenghtMap; i++)
            {    
                for (int j = 0; j < par_model.MyWorld.LenghtMap; j++)
                {

                    par_model.MyWorld.MapElement[i, j].InitialisationNeighbors(par_model.MyWorld.MapElement, par_model.MyWorld.LenghtMap);
                }
              
            }

            // par_model.MyWorld.InitialisationRocks();

            //mapping des elements 2D
            for (int i = 0; i < par_model.MyWorld.LenghtMap; i++)
            {
                for (int j = 0; j < par_model.MyWorld.LenghtMap; j++)
                {
                 
                    Element2D loc_element = new Element2D(par_model.MyWorld.MapElement[i, j]);

                    loc_element = TestElement(par_model.MyWorld.MapElement[i, j]);
                    loc_element.X = m_x + i * loc_element.Length + i * m_limit_grid;
                    loc_element.Y = m_y + j * loc_element.Length + j * m_limit_grid;


                    loc_element.Draw(this);

                    m_list_elements.Add(loc_element);

                }

            }

           
         
           for (int i=0; i< m_list_elements.Count; i++)
            {
                if (i == 8)
                {
                    m_list_elements[i].Shape.BackColor = System.Drawing.Color.Black;

                    if (SearchElement(m_list_elements[i].ModelElement.East) != null)
                        SearchElement(m_list_elements[i].ModelElement.East).Shape.BackColor = System.Drawing.Color.White;

                    if (SearchElement(m_list_elements[i].ModelElement.North) != null)
                        SearchElement(m_list_elements[i].ModelElement.North).Shape.BackColor = System.Drawing.Color.White;
                   
                    if (SearchElement(m_list_elements[i].ModelElement.NorthEast) != null)
                        SearchElement(m_list_elements[i].ModelElement.NorthEast).Shape.BackColor = System.Drawing.Color.White;

                    if (SearchElement(m_list_elements[i].ModelElement.South) != null)
                        SearchElement(m_list_elements[i].ModelElement.South).Shape.BackColor = System.Drawing.Color.White;

                    if (SearchElement(m_list_elements[i].ModelElement.SouthEast) != null)
                        SearchElement(m_list_elements[i].ModelElement.SouthEast).Shape.BackColor = System.Drawing.Color.White;

                    if (SearchElement(m_list_elements[i].ModelElement.SouthWest) != null)
                        SearchElement(m_list_elements[i].ModelElement.SouthWest).Shape.BackColor = System.Drawing.Color.White;

                    if (SearchElement(m_list_elements[i].ModelElement.NorthWest) != null)
                        SearchElement(m_list_elements[i].ModelElement.NorthWest).Shape.BackColor = System.Drawing.Color.White;

                    if (SearchElement(m_list_elements[i].ModelElement.West) != null)
                        SearchElement(m_list_elements[i].ModelElement.West).Shape.BackColor = System.Drawing.Color.White;
                    

                }

                if (i%35==0)
                {
                    ItemMove2D loc_item = new ItemMove2D( new Model.ItemMove(m_list_elements[i].ModelElement, 1, "OGC") );
                    loc_item.X = m_list_elements[i].X;
                    loc_item.Y = m_list_elements[i].Y;
                    loc_item.Draw(this);
                    loc_item.Shape.BringToFront();

                    m_item_move.Add(loc_item);
                }
            }

          

        }


        private Element2D SearchElement(Model.Element par_item) 
        {
            foreach (Element2D item in m_list_elements)
                if (item.ModelElement == par_item)
                    return item;
            return null;
        }

        private View._2D.Element2D TestElement(object o)
        {
            if (o is Model.Rock)
                return new View._2D.Rock2D((Model.Element)o);
            else
                return new View._2D.Field2D((Model.Element)o);
        }

        private void Run()
        {

        }

        private void timer_Tick(object sender, EventArgs e)
        {
            foreach (ItemMove2D item in m_item_move)
            {
                item.MyItem.MoveRandom();
              
              //  m_point.X = m_x + item.MyItem.PositionNow.I * item.Length + item.MyItem.PositionNow.I * 1;
              //  m_point.Y = m_y + item.MyItem.PositionNow.J * item.Length + item.MyItem.PositionNow.J * 1;

              //  item.Shape.Location = m_point;
                item.Shape.Location = new Point(m_x + item.MyItem.PositionNow.I * item.Length + item.MyItem.PositionNow.I * m_limit_grid,
                                                m_y + item.MyItem.PositionNow.J * item.Length + item.MyItem.PositionNow.J * m_limit_grid);
                   
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
